package com.sketch.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.entity.VipPrivilege;
import com.sketch.entity.dto.VipPrivilegeDTO;
import com.sketch.mapper.VipPrivilegeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VipPrivilegeService {
    @Resource
    private VipPrivilegeMapper vipPrivilegeMapper;

    public Result addOrUpdate( VipPrivilegeDTO vipPrivilegeDTO){
        if(ObjectUtils.isEmpty(vipPrivilegeDTO.getId())){
            VipPrivilege vipPrivilege = new VipPrivilege();
            BeanUtils.copyProperties(vipPrivilegeDTO,vipPrivilege);
            vipPrivilegeMapper.insert(vipPrivilege);
        }else {
            VipPrivilege vipPrivilege = new VipPrivilege();
            BeanUtils.copyProperties(vipPrivilegeDTO,vipPrivilege);
            vipPrivilegeMapper.updateById(vipPrivilege);
        }

        return Result.success();
    }
    public Result list( Integer page, Integer size){
        Page<VipPrivilege> sketchInfoPage = new Page<>(page, size);
        IPage<VipPrivilege> result = vipPrivilegeMapper.selectPage(sketchInfoPage,null);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<VipPrivilegeDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            VipPrivilegeDTO vipPrivilegeDTO = new VipPrivilegeDTO();
            BeanUtils.copyProperties(e,vipPrivilegeDTO);
            list.add(vipPrivilegeDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id){
        VipPrivilege vipPrivilege = vipPrivilegeMapper.selectById(id);
        VipPrivilegeDTO vipPrivilegeDTO = new VipPrivilegeDTO();
        BeanUtils.copyProperties(vipPrivilege,vipPrivilegeDTO);
        return Result.success().withData(vipPrivilegeDTO);
    }

    public Result delete(Long id){
        vipPrivilegeMapper.deleteById(id);
        return Result.success();
    }
}
